package responsibilitychain;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author zenghy
 **/
@Slf4j
public class FilterChain implements IFilter {

    private static List<IFilter> filters = new ArrayList<>();

    static {
        filters.add(new FemaleFilter());
        filters.add(new MaleFilter());
        filters.add(new BothFilter());
    }

    @Override
    public boolean doFilter(IRequest request) {
        AtomicBoolean result = new AtomicBoolean(true);
        filters.stream()
                .filter(f -> f.support(request.getClass()))
                .filter(f -> result.get())
                .forEach(f -> result.set(f.doFilter(request)));
        return result.get();
    }

    @Override
    public boolean support(Class<? extends IRequest> clz) {
        return false;
    }
}
